package com.sugon.controller.spare;

import com.sugon.annotation.AutoLog;
import com.sugon.annotation.NonBusinessParamValid;
import com.sugon.entity.SparePartDevEntity;
import com.sugon.modules.device.model.vo.DevSparepartCorrelationParamVO;
import com.sugon.modules.device.service.IDeviceManager;
import com.sugon.service.SparePartDevService;
import com.sugon.utils.PageUtils;
import com.sugon.utils.Query;
import com.sugon.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 设备备件关联Controller
 *
 * @author sugon
 * @date 2019-07-18 10:18:31
 */
@Controller
@RequestMapping("sparepartdev")
public class SparePartDevController {

    @Autowired
    private SparePartDevService sparePartDevService;
    @Autowired
    private IDeviceManager deviceManager;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sparepartdev:list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<SparePartDevEntity> sparePartDevList = sparePartDevService.queryList(query);
        int total = sparePartDevService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(sparePartDevList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 根据备件查询关联设备信息
     *
     * @param params
     * @return
     */
    @RequestMapping("/listBySpare")
    @ResponseBody
    public R listBySpare(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<SparePartDevEntity> sparePartDevList = sparePartDevService.queryListBySpare(query);
        int total = sparePartDevService.querySpareTotal(query);

        PageUtils pageUtil = new PageUtils(sparePartDevList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sparepartdev:info")
    @ResponseBody
    public R info(@PathVariable("id") String id) {
        SparePartDevEntity sparePartDev = sparePartDevService.queryObject(id);

        return R.ok().put("sparePartDev", sparePartDev);
    }

    /**
     * 保存
     */
    @NonBusinessParamValid
    @RequestMapping("/save")
    @RequiresPermissions("sparepartdev:save")
    @ResponseBody
    public R save(@RequestBody @Validated DevSparepartCorrelationParamVO devSparepartCorrelationParam, BindingResult bindingResult) {
        return deviceManager.addDevSparepartCorrelation(devSparepartCorrelationParam) ? R.ok() : R.error();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sparepartdev:update")
    @ResponseBody
    public R update(@RequestBody SparePartDevEntity sparePartDev) {
        sparePartDevService.update(sparePartDev);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sparepartdev:delete")
    @ResponseBody
    public R delete(@RequestBody String[] ids) {
        sparePartDevService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<SparePartDevEntity> list = sparePartDevService.queryList(params);

        return R.ok().put("list", list);
    }

    /**
     * 批量审核设备-备件关联记录
     */
    @RequestMapping("/batchReview")
    @RequiresPermissions("sparepartdev:review")
    @ResponseBody
    public R review(@RequestBody List<String> sparePartDevIdList) {
        return sparePartDevService.batchReview(sparePartDevIdList) > 0 ? R.ok() : R.error();
    }

    /**
     * 设备查看备件列表
     */
    @RequestMapping("/devlist")
    @ResponseBody
    public R devList(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<SparePartDevEntity> sparePartDevList = sparePartDevService.devQueryList(query);
        int total = sparePartDevService.devQueryTotal(query);

        PageUtils pageUtil = new PageUtils(sparePartDevList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }
}
